#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\rtc.h"

void __irq Rtc_Tick(void);
void __irq Rtc_display(void);
char *date[8] = {"","SUN","MON","TUE","WED","THU","FRI","SAT"};
volatile unsigned int sec_tick;
int year,month,day,weekday,hour,min,sec;

void Display_Rtc(void)
{
      
    Uart_Printf("This test should be excuted once RTC test(Alarm) for RTC initialization\n");
    Uart_Printf("Please check the VDDRTC connector or connect the VDD to Bt1+ !!!\n");	
    Uart_Printf("Typing any key to exit!!!\n");
    Rtc_Init(); 
    pISR_TICK=(unsigned)Rtc_display;

    rRTCCON = 0x01;    // R/W enable, 1/32768, Normal(merge), No reset
    sec_tick=1;
    rINTMSK=~(BIT_GLOBAL|BIT_TICK); 
    
    rTICINT = 127+(1<<7);	//START

    Uart_Getch();
    rINTMSK |= (BIT_GLOBAL | BIT_TICK);
    rRTCCON=0x0;	//END
}

void __irq Rtc_display(void)
{
    rI_ISPC=BIT_TICK;   
    if(rBCDYEAR == 0x99) 
	    year = 0x1999;
	else 
	    year = 0x2000 + rBCDYEAR;
	    month=rBCDMON;
	    day=rBCDDAY;
	    weekday=rBCDDATE;
	    hour=rBCDHOUR;
	    min=rBCDMIN;
	    sec=rBCDSEC;
	    
   Uart_Printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4x,%2x,%2x,%s,%2x:%2x:%2x",year,month,day,date[weekday],hour,min,sec);
}

void DisplayRtc(void)
{
  
  rRTCCON = 0x01;    // R/W enable, 1/32768, Normal(merge), No reset
  Rtc_Init();
  while(1)
  {
	  if(rBCDYEAR == 0x99) 
	  {
	    year = 0x1999;
	  }
	  else 
	  {
	    year = 0x2000 + rBCDYEAR;
	  }
	  
	  month=rBCDMON;
	  day=rBCDDAY;
	  weekday=rBCDDATE;
	  hour=rBCDHOUR;
	  min=rBCDMIN;
	  sec=rBCDSEC;
	  if(sec!=0)
	    break;
  }	 
  Uart_Printf("\n%4x,%2x,%2x,%s,%2x:%2x:%2x\n",year,month,day,date[weekday],hour,min,sec);
  rRTCCON = 0x00;    // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
}
volatile int isRtcInt;

void __irq Rtc_Int(void)
{
    rI_ISPC=BIT_RTC;   
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
    Uart_Printf("RTC Alarm Interrupt O.K.\n");
    isRtcInt=1;  
}


int Test_Rtc_Alarm(void)  
{
    Uart_Printf("[RTC Alarm Test for S3C44B0X]\n");
    Uart_Printf("Please check the VDDRTC connector or connect the VDD to Bt1+ !!!\n");

    Rtc_Init();

    rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset

    rALMYEAR=rBCDYEAR ;
    rALMMON =rBCDMON;
    rALMDAY =rBCDDAY  ;
    rALMHOUR=rBCDHOUR ;
    rALMMIN =rBCDMIN  ;
    if(rBCDSEC<0X30)
    {
    rALMSEC =0x30;}
    else
    {
    rALMSEC =0x59;}

/*    rALMYEAR=TESTYEAR2 ;
    rALMMON =TESTMONTH2;
    rALMDAY =TESTDAY2  ;
    rALMHOUR=TESTHOUR2 ;
    rALMMIN =TESTMIN2  ;
    rALMSEC =TESTSEC2+9; */

    isRtcInt=0;
    pISR_RTC=(unsigned int)Rtc_Int;
    rRTCALM=0x7f;
    rINTMSK=~(BIT_GLOBAL|BIT_RTC);

    while(isRtcInt==0);
    
    rINTMSK|=BIT_GLOBAL;
    rRTCCON = 0x0;    // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
    return 1;
}


void Rtc_Init(void)
{
    rRTCCON = 0x01;	// R/W enable, 1/32768, Normal(merge), No reset

    rBCDYEAR = TESTYEAR;
    rBCDMON  = TESTMONTH;
    rBCDDAY  = TESTDAY;	// SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
    rBCDDATE = TESTDATE;
    rBCDHOUR = TESTHOUR;
    rBCDMIN  = TESTMIN;
    rBCDSEC  = TESTSEC;
}


void Test_Rtc_Tick(void)
{
    Uart_Printf("[RTC Tick interrupt test for S3C44B0X]\n");
    Uart_Printf("Please check the VDDRTC connector or connect the VDD to Bt1+ !!!\n");
    Uart_Printf("Typing any key to exit!!!\n");	 
    pISR_TICK=(unsigned)Rtc_Tick;


    sec_tick=1;
    rINTMSK=~(BIT_GLOBAL|BIT_TICK); 
    rRTCCON=0x0;		//R/W disable(for power consumption), 1/32768, Normal(merge), No reset
    rTICINT = 127+(1<<7);	//START

    Uart_Getch();
    rINTMSK |= (BIT_GLOBAL | BIT_TICK);

    rRTCCON=0x0;	//END
}


void __irq Rtc_Tick(void)
{
    rI_ISPC=BIT_TICK;   
    Uart_Printf("\b\b\b\b\b\b\b%03d sec",sec_tick++);
    //Uart_Printf("%03d sec\r",sec_tick++);
}

